<?php
class Cms_String_String{
	
	
	public static function transferData($string){
		$trans = array(
				'à'=>'a','á'=>'a','ả'=>'a','ã'=>'a','ạ'=>'a',
				'ă'=>'a','ằ'=>'a','ắ'=>'a','ẳ'=>'a','ẵ'=>'a','ặ'=>'a',
				'â'=>'a','ầ'=>'a','ấ'=>'a','ẩ'=>'a','ẫ'=>'a','ậ'=>'a',
				'À'=>'a','Á'=>'a','Ả'=>'a','Ã'=>'a','Ạ'=>'a',
				'Ă'=>'a','Ằ'=>'a','Ắ'=>'a','Ẳ'=>'a','Ẵ'=>'a','Ặ'=>'a',
				'Â'=>'a','Ầ'=>'a','Ấ'=>'a','Ẩ'=>'a','Ẫ'=>'a','Ậ'=>'a',
				'đ'=>'d','Đ'=>'d',
				'è'=>'e','é'=>'e','ẻ'=>'e','ẽ'=>'e','ẹ'=>'e',
				'ê'=>'e','ề'=>'e','ế'=>'e','ể'=>'e','ễ'=>'e','ệ'=>'e',
				'È'=>'e','É'=>'e','Ẻ'=>'e','Ẽ'=>'e','Ẹ'=>'e',
				'Ê'=>'e','Ề'=>'e','Ế'=>'e','Ể'=>'e','Ễ'=>'e','Ệ'=>'e',
				'ì'=>'i','í'=>'i','ỉ'=>'i','ĩ'=>'i','ị'=>'i',
				'Ì'=>'i','Í'=>'i','Ỉ'=>'i','Ĩ'=>'i','Ị'=>'i',
				'ò'=>'o','ó'=>'o','ỏ'=>'o','õ'=>'o','ọ'=>'o',
				'ô'=>'o','ồ'=>'o','ố'=>'o','ổ'=>'o','ỗ'=>'o','ộ'=>'o',
				'ơ'=>'o','ờ'=>'o','ớ'=>'o','ở'=>'o','ỡ'=>'o','ợ'=>'o',
				'Ò'=>'o','Ó'=>'o','Ỏ'=>'o','Õ'=>'o','Ọ'=>'o',
				'Ô'=>'o','Ồ'=>'o','Ố'=>'o','Ổ'=>'o','Ỗ'=>'o','Ộ'=>'o',
				'Ơ'=>'o','Ờ'=>'o','Ớ'=>'o','Ở'=>'o','Ỡ'=>'o','Ợ'=>'o',
				'ù'=>'u','ú'=>'u','ủ'=>'u','ũ'=>'u','ụ'=>'u',
				'ư'=>'u','ừ'=>'u','ứ'=>'u','ử'=>'u','ữ'=>'u','ự'=>'u',
				'Ù'=>'u','Ú'=>'u','Ủ'=>'u','Ũ'=>'u','Ụ'=>'u',
				'Ư'=>'u','Ừ'=>'u','Ứ'=>'u','Ử'=>'u','Ữ'=>'u','Ự'=>'u',
				'ỳ'=>'y','ý'=>'y','ỷ'=>'y','ỹ'=>'y','ỵ'=>'y',
				'Y'=>'y','Ỳ'=>'y','Ý'=>'y','Ỷ'=>'y','Ỹ'=>'y','Ỵ'=>'y'
		);
		$string = strtr($string,$trans);
		return $string;
	}
	public static function toLower($string, $encode='UTF-8'){
		$string = mb_strtolower($string, $encode);
		return $string;
	}
	
	public static function toAlias($string){
		$tmp = array("~","`","!","@","#","$","%","^","&","*","(",")","-","_","=","+","{","[","]","}","|","\\",":",";","'","\"","<",",",">",".","?","/");		
		$string = self::transferData($string);
		$string = str_replace($tmp, " ", $string);
		$string = trim($string);
		$arr = explode(" ", $string);//tạo mảng
		$string = "";
		foreach($arr as $key){//nối chuỗi
			if(!empty($key))
				$string .="-".$key;
		}
		$string=self::toLower(substr($string, 1));
		return $string;	
	}
	
	
	// format string date example: 14/2/2014
	public static function getIntFromDate($stringDate){
		if(is_string($stringDate)){
			
			//Kiểm tra đúng định dạng hay ko?
			if(!preg_match('/[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{1,4}/', $stringDate)){
				return null;//nếu sai
			}else{
				$arr = explode("/", $stringDate);
				return ($stringDate <= 0) ? null: mktime(23,59,59,$arr[1],$arr[0],$arr[2]);
			}
		}
		return null;
	}
	
	
	//chuyển về định dạng datetime của mysql
	//$offset = "early or last"
	public static function getDateFromString($string,$offset = "early"){
		if(!preg_match('/[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{1,4}/', $string)){
				return null; //nếu sai
		}else{
			
			//tách chuỗi
			$arr = explode("/", $string);
			
			//nếu là thời gian bắt đầu
			if($offset=="early"){
				return "$arr[2]-$arr[1]-$arr[0] 00:00:00";
			}
			return "$arr[2]-$arr[1]-$arr[0] 23:59:59";
		}
		
	}
}
















